/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Table;

import BLL.Customer_BLL;
import BLL.Unity;
import BLL.SavingBook_BLL;
import BLL.SavingType_BLL;
import DTO.Customer_DTO;
import DTO.SavingBook_DTO;
import DTO.SavingType_DTO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author Nguyen Phi Hung
 */
public class table_SavingbookModel extends AbstractTableModel{
    
    private int rows, columns;
    private JXTable table;
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return table.getValueAt(rows, columns);
    }
    
    public Date getDatefromDatabase(int rowIndex) throws ParseException{
        String date = (String) getTable().getValueAt(rowIndex, 5);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");        
        Date s = new Date() ;
        s = format.parse(date);
        return s;
    }
    
    public void loadItemIntoComboBox(JComboBox jcom, int rowIndex) throws Exception{           
        String ID = this.table.getValueAt(rowIndex, 0).toString();
        SavingBook_BLL saving_bll = new SavingBook_BLL();
        SavingBook_DTO savingbook = new SavingBook_DTO();
        savingbook = saving_bll.getSavingBookByID(ID);
        
        SavingType_DTO savingtype = new SavingType_DTO();
        SavingType_BLL stype_bll = new SavingType_BLL();
        savingtype = stype_bll.getSavingType_byTypeID(savingbook.getTypeID());
        
        jcom.addItem(savingtype.getName());
    }
    
    public Vector getValueAtRow(int rowIndex){
        Vector vtdata = new Vector();
        for(int i = 0; i <table.getColumnCount(); i++)
            vtdata.add(getTable().getValueAt(rowIndex, i).toString());
        return vtdata;        
    }   
    
    public DefaultTableModel loadTable() throws Exception{
        SavingBook_BLL bll = new SavingBook_BLL();
        HashSet<SavingBook_DTO> savingbooks;
        savingbooks = bll.getSavingBooks(null,"BookID");
        Vector vtColumn = new Vector();
        Vector vtData = new Vector();
        
        vtColumn.add("Mã sổ");
        vtColumn.add("Tên chủ sổ");
        vtColumn.add("Số CMND");
        vtColumn.add("Địa chỉ");
        vtColumn.add("Loại tiết kiệm");
        vtColumn.add("Ngày mở sổ");
        vtColumn.add("Số tiền dư");
        
        for(int i = 0; i < savingbooks.size(); i++ ){            
            Customer_DTO customer = new Customer_DTO();
            Customer_BLL customer_bll = new Customer_BLL();
            SavingType_DTO savingtype = new SavingType_DTO();
            SavingType_BLL savingtype_bll = new SavingType_BLL();            
            Vector vtRow = new Vector();
            
            SavingBook_DTO savingbook = bll.getSavingBookByIndex(savingbooks, i);
            //lay thong tin ma so
            vtRow.addElement(savingbook.getBookID());

            //lay thong tin  chu so tiet kiem bang CustomerID
//            HashSet<Customer_DTO> customers = customer_bll.getCustomers(null);
//            customer = customer_bll.getCustomerByID(customers,savingbook.getCustomerID());
//            vtRow.addElement(customer.getFullName());
//            vtRow.addElement(customer.getIDNumber());
//            vtRow.addElement(customer.getHomeAddress());
            
            //lay gia tri 
            savingtype = savingtype_bll.getSavingType_byTypeID(savingbook.getTypeID());
            vtRow.addElement(savingtype.getName());
            Unity unity = new Unity();
            // lay ngay mo so
            vtRow.addElement(unity.toString(savingbook.getOpeningDate()));
            //vtRow.addElement(savingbook.get);
            vtRow.addElement(savingbook.getAccountBalance());
            
            vtData.addElement(vtRow);
            }     
        return new DefaultTableModel(vtData, vtColumn);        
    }

    @Override
    public int getRowCount() {
        return this.rows;
    }

    @Override
    public int getColumnCount() {
        return this.columns;
    }

    /**
     * @return the rows
     */
    public int getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * @return the columns
     */
    public int getColumns() {
        return columns;
    }

    /**
     * @param columns the columns to set
     */
    public void setColumns(int columns) {
        this.columns = columns;
    }

    /**
     * @return the table
     */
    public JXTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(JXTable table) {
        this.table = table;
    }
}
